<template>
    <McLayout class="container">
        <McHeader :title="'流式交互示例'" :logoImg="'/src/assets/logo.svg'">
            <template #operationArea>
                <div class="operations">
                    <i class="icon-helping"></i>
                </div>
            </template>
        </McHeader>
        <McLayoutContent v-if="startPage"
            style="display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px">
            <McIntroduction :logoImg="'/src/assets/logo.svg'" :title="'交互展示'" :subTitle="'Vue3 GPT流式交互示例，10分钟搭建起来业务'"
                :description="description"></McIntroduction>
            <McPrompt :list="introPrompt.list" :direction="introPrompt.direction" class="intro-prompt"
                @itemClick="onSubmit($event.label)"></McPrompt>
        </McLayoutContent>
        <McLayoutContent class="content-container" v-else>
            <template v-for="(msg, idx) in messages" :key="idx">
                <McBubble v-if="msg.from === 'user'" :content="msg.content" :align="'right'"
                    :avatarConfig="{ imgSrc: '/src/assets/logo.svg' }">
                </McBubble>
                <McBubble v-else :loading="msg.loading ?? false" :avatarConfig="{ imgSrc: '/src/assets/logo.svg' }"
                    :variant="'bordered'">
                    <McMarkdownCard :content="msg.content"></McMarkdownCard>
                </McBubble>
            </template>
        </McLayoutContent>
        <div class="shortcut" style="display: flex; align-items: center; gap: 8px">
            <McPrompt v-if="!startPage" :list="simplePrompt" :direction="'horizontal'" style="flex: 1"
                @itemClick="onSubmit($event.label)"></McPrompt>
            <Button style="margin-left: auto" icon="add" shape="circle" title="新建对话" size="md"
                @click="newConversation" />
        </div>
        <McLayoutSender>
            <McInput :value="inputValue" :maxLength="2000" @change="(e) => (inputValue = e)" @submit="onSubmit">
                <template #extra>
                    <div class="input-foot-wrapper">
                        <div class="input-foot-left">
                            <span v-for="(item, index) in inputFootIcons" :key="index">
                                <i :class="item.icon"></i>
                                {{ item.text }}
                            </span>
                            <span class="input-foot-dividing-line"></span>
                            <span class="input-foot-maxlength">{{ inputValue.length }}/2000</span>
                        </div>
                        <div class="input-foot-right">
                            <Button icon="op-clearup" shape="round" :disabled="!inputValue"
                                @click="inputValue = ''"><span class="demo-button-content">清空输入</span></Button>
                        </div>
                    </div>
                </template>
            </McInput>
        </McLayoutSender>
    </McLayout>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { Button } from 'vue-devui/button';

import 'vue-devui/button/style.css';
import { usePostSSE } from '@/utils/usePostSSE'
const description = [
    'vue3-gpt交互及样式示例， 前端同学抄作业',
    '有前端内推可以联系我，VX：Wu_DaDabao',
];
const introPrompt = {
    direction: 'horizontal',
    list: [
        {
            value: 'quickSort',
            label: '获取示例代码',
            iconConfig: { name: 'icon-star', color: 'rgb(255, 215, 0)' },
            desc: '点击跳转gitee仓库',
        },
        {
            value: 'helpMd',
            label: '你可以帮我做些什么？',
            iconConfig: { name: 'icon-info-o', color: '#5e7ce0' },

            desc: '了解当前大模型可以帮你做的事',
        },
        {
            value: 'bindProjectSpace',
            label: '怎么绑定项目空间',
            iconConfig: { name: 'icon-priority', color: '#3ac295' },
            desc: '如何绑定云空间中的项目',
        },
    ],
};
const simplePrompt = [
    {
        value: 'quickSort',
        iconConfig: { name: 'icon-info-o', color: '#5e7ce0' },
        label: '帮我写一个快速排序',
    },
    {
        value: 'helpMd',
        iconConfig: { name: 'icon-star', color: 'rgb(255, 215, 0)' },
        label: '你可以帮我做些什么？',
    },
];
const startPage = ref(true);
const inputValue = ref('');
const inputFootIcons = [
    { icon: 'icon-at', text: '智能体' },
    { icon: 'icon-standard', text: '词库' },
    { icon: 'icon-add', text: '附件' },
];

const messages = ref<any[]>([]);

const newConversation = () => {
    startPage.value = true;
    messages.value = [];
}
const { start, stop } = usePostSSE()
const onSubmit = (evt) => {

    if (evt == '获取示例代码') {

        window.open('https://gitee.com/dabao1214/vue3-gpt-stream', "_blank")

        return
    }

    inputValue.value = '';
    startPage.value = false;
    // 用户发送消息
    messages.value.push({
        from: 'user',
        content: evt,
    });
    start({
        url: 'http://wudabao.natapp1.cc/api/chat',
        body: { message: evt },
        onMessage: ({ status, data }) => {
            if (status === 'message') {
                if (messages.value[messages.value.length - 1].from === 'user') {
                    messages.value.push({ from: 'model', content: data.message, })
                } else {
                    messages.value[messages.value.length - 1].content = data.message;
                }

            }
        },
    })

};
</script>

<style>
.container {
    width: 1000px;
    margin: 20px auto;
    height: calc(100vh - 82px);
    padding: 20px;
    gap: 8px;
    background: #fff;
    border: 1px solid #ddd;
    border-radius: 16px;
}

.mc-header-logo {
    width: 32px;
    height: 32px;
}

.mc-introduction-logo-container>img {
    width: 32px;
    height: 32px;
}

.content-container {
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: auto;
}

.input-foot-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 100%;
    margin-right: 8px;

    .input-foot-left {
        display: flex;
        align-items: center;
        gap: 8px;

        span {
            font-size: 14px;
            line-height: 18px;
            color: #252b3a;
            cursor: pointer;
        }

        .input-foot-dividing-line {
            width: 1px;
            height: 14px;
            background-color: #d7d8da;
        }

        .input-foot-maxlength {
            font-size: 14px;
            color: #71757f;
        }
    }

    .input-foot-right {
        .demo-button-content {
            font-size: 14px;
        }

        &>*:not(:first-child) {
            margin-left: 8px;
        }
    }
}
</style>